package com.wno704.system.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.ReversedLinesFileReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName FileUtil
 * @Description TODO
 * @Author wno704
 * @Date 2022/10/12 22:33
 * @Version 1.0
 */
public class FileUtil {

    private FileUtil(){

    }

    /**
     * @Description: 读取一个文件行数
     * @Param: [file]
     * @return: long
     * @Author: wno704
     * @Date: 2022/10/12
     */

    public static long getLineNumber(File file) {
        if (file.exists()) {
            try {
                FileReader fileReader = new FileReader(file);
                LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
                lineNumberReader.skip(Long.MAX_VALUE);
                long lines = lineNumberReader.getLineNumber() + 1;
                fileReader.close();
                lineNumberReader.close();
                return lines;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    /**
     * @Description: 读取日志最后N行
     * @Param: [path, s, numLastLineToRead]
     * @return: java.util.List<java.lang.String>
     * @Author: wno704
     * @Date: 2022/10/12
     */

    public static List<String> readLastLine(File file, Charset s, int numLastLineToRead) {
        List<String> result = new ArrayList<>();
        try (ReversedLinesFileReader reader = new ReversedLinesFileReader(file, s)) {
            String line = "";
            while ((line = reader.readLine()) != null && result.size() < numLastLineToRead) {
                result.add(line);
            }
            //倒叙遍历
            Collections.reverse(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Description: 读取日志最前N行
     * @Param: [path, s, numLastLineToRead]
     * @return: java.util.List<java.lang.String>
     * @Author: wno704
     * @Date: 2022/10/12
     */
    public static List<String> readFirstLine(File file, Charset s, int numFirstLineToRead) {
        List<String> result = new ArrayList<>();
        try  {
            long total = getLineNumber(file);
            if (numFirstLineToRead > total){
                numFirstLineToRead = (int)total;
            }
            List<String> lines = FileUtils.readLines(file,s);
            result = lines.subList(0,numFirstLineToRead);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Description: 读取日志n-m行
     * @Param: [path, s, numLastLineToRead]
     * @return: java.util.List<java.lang.String>
     * @Author: wno704
     * @Date: 2022/10/12
     */
    public static List<String> readSubLine(File file, Charset s, int numStart, int numLenth) {
        List<String> result = new ArrayList<>();
        try  {
            long total = getLineNumber(file);
            if (numStart > total){
                return null;
            }
            if ((numStart+numLenth) > total){
                return readLastLine(file,s,(int)(total-numStart+1));
            }
            List<String> lines = FileUtils.readLines(file,s);
            result = lines.subList(numStart-1,numStart-1+numLenth);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Description: 读取文件指定行
     * @Param: [path, s, lineArray]
     * @return: java.util.List<java.lang.String>
     * @Author: wno704
     * @Date: 2022/10/12
     */
    public static List<String> readAppointedLineNumberArray(File file, Charset s, int[] lineArray) {
        List<String> result = new ArrayList<>();
        try  {
            Arrays.sort(lineArray);
            long total = getLineNumber(file);
            List<String> lines = FileUtils.readLines(file,s);

            for (int line : lineArray){
                if (line > total){
                    break;
                }
                result.add(lines.get(line-1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean deleteFile(String filePath)
    {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists())
        {
            file.delete();
            flag = true;
        }
        return flag;
    }

    public static void deleteFiles(String clientBase) {
        File file = new File(clientBase);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for(File item: files) {
                if (item.isDirectory()) {
                    System.out.println("进入目录：" + item.getPath());
                    deleteFiles(item.getPath()); //继续读取目录下文件或文件夹
                } else {
                    System.out.println("删除文件：" + item.getPath());
                    item.delete();
                }
            }
            System.out.println("删除目录：" + file.getPath());
            file.delete();
        } else {
            System.out.println("删除文件：" + file.getPath());
            file.delete();
        }
    }
}
